//
//  ContentAaaVC.m
//  SideDemo
//
//  Created by zsp on 16/4/8.
//  Copyright © 2016年 zsp. All rights reserved.
//

#import "ContentAaaVC.h"
#import "ContentBbbVC.h"
#import <Masonry.h>

@interface ContentAaaVC ()<UIPageViewControllerDataSource>

@property(nonatomic, strong)UIPageViewController *pageVC;
@property(nonatomic, strong)NSMutableArray *datas;

@property(nonatomic, assign)NSUInteger curIndex;


@end

@implementation ContentAaaVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (void)configView {
    
    self.pageVC=[[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    
    self.pageVC.dataSource=self;
    self.pageVC.delegate=self;
    
    self.datas=[NSMutableArray array];
    
    for(int i=0;i<4;i++){
        ContentBbbVC *bbbVC=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ContentBbbVC"];
        bbbVC.content=[NSString stringWithFormat:@"ContentBbbVC-------%d",i];
        [self.datas addObject:bbbVC];
    }
    
    self.curIndex=0;
    [self.pageVC setViewControllers:@[[self.datas firstObject]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    [self addChildViewController:self.pageVC];
    
    
    
    
    [self.view addSubview:self.pageVC.view];
    
    [self.pageVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
//        if (self.navigationController) {
//            make.top.equalTo(self.view).with.offset(64);
//        }else{
            make.top.equalTo(self.view);
//        }
    }];
    
    [self.view setBackgroundColor:[UIColor redColor]];
    [self.pageVC.view setBackgroundColor:[UIColor purpleColor]];
}

- (void)dealloc {
    NSLog(@"%s - %s",__FILE__, __func__);
}

#pragma mark -
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSLog(@"%s", __func__);
    NSInteger index=[self.datas indexOfObject:viewController];
    if (index > 0 ) {
        return [self.datas objectAtIndex:index-1];
    }
    return nil;
}
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSLog(@"%s", __func__);
    NSInteger index=[self.datas indexOfObject:viewController];
    
    if (index < self.datas.count-1) {
        return [self.datas objectAtIndex:index+1];
    }
    return nil;
}
  
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0) {
    NSLog(@"%s", __func__);
    return self.datas.count;
}
//- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0) {
//    NSLog(@"%s", __func__);
//    return 0;
//}

#pragma mark -UIPageViewControllerDelegate
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    NSLog(@"%s", __func__);
    if (completed) {
        NSLog(@"completed, curIndex = %lu", (unsigned long)self.curIndex);
    }
}

@end
